<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Warehouses extends CI_Controller {

	public function index()
	{
		$this->load->model('Warehouses_model');
	    $id = null;
	    $name = null;
	    $text = null;
	    if ($this->input->post('id')) {
	        $id = $this->input->post('id');
	    }
	    if ($this->input->post('name')) {
	        $name = $this->input->post('name');
	    }
	    if ($this->input->post('text')) {
	        $text = $this->input->post('text');
	    }
	    $warehouses = $this->Warehouses_model->getAll();
	    $supplier = $this->Warehouses_model->getAll();
	    if ($id != null || $name != null || $text != null) {
	        $warehouses = $this->Warehouses_model->search($id, $name, $text);
	    }
	    $vars['warehouses'] = $warehouses;
	     $vars['supplier'] = $supplier;
	    $vars['id'] = $id;
	    $vars['name'] = $name;
	    $vars['text'] = $text;
	    $vars['content_view'] = 'warehouses_list_index';
	      $vars['content_view'] = 'warehouses_xproducts';
	      $vars['content_view'] = 'warehouses_list_index';
	    $vars['menu'] = 'warehouses';
	    $this->load->view('template', $vars);
	}

	public function add()
	{
	    $this->load->model('Warehouses_model');
	    $vars['content_view'] = 'warehouses_form';
	    $vars['menu'] = 'warehouses';
	    $this->load->view('template', $vars);
	}

	public function submit()
	{
	    $id = $this->input->post('warehouse_id');
	    if ($this->_validate_submit() === FALSE) {
	        if (isset($id) && is_numeric($id)) {
	            $this->edit($id);
	        } else {
	         $this->add();
	        }
	        return;
	    } else {

	        $this->load->model('Warehouses_model');
	        $data = array();
	        $data['name'] = $this->input->post('name');
	        $data['text'] = $this->input->post('text');
	        $data['status'] = 1;
	    }
	    if (isset($id) && is_numeric($id)) {
	        $this->Warehouses_model->update($id, $data);
	    } else {
	        $this->Warehouses_model->add($data);
	        if(!($this->input->post('id_sub_warehouses'))){                  
           $this->load->model('Products_model');
           $this->load->model('Warehouses_model');        
           $contenaumentador = $this->input->post('contenaumentador');
                     // $datei = $this->input->post('date');   	
   for($i = 1 ; $i <= $contenaumentador; $i++){  

               $data = array( 'name' => $this->input->post('name_'.$i.''),	
               				 'place' => $this->input->post('place_'.$i.''),
                'id_warehouses' => $this->input->post('id_warehouses'),
                );
                $this->Warehouses_model->add2($data); 
			}

	    }
	}




/////////////
	    


	    redirect('/warehouses/', 'refresh');
	}

	public function edit($code)
	{
            $this->load->model('warehouses_list_index_model');	 
            $this->load->model('Warehouses_model');	    
	    $vars['warehouse'] = $this->Warehouses_model->find($code);
            $vars['subwarehouses'] = $this->warehouses_list_index_model->find_subalmacen($code);
	    $vars['content_view'] = 'warehouses_form';
            $vars['datesale'] = 'warehouses';	    
	    $vars['menu'] = 'warehouses';
	    $this->load->view('template', $vars);
	}
        
        public function subwarehouses() {
        
        $name = null;
        $place = null;
        $this->load->model('Products_model');
        
        if ($this->input->post('name')) {
            $name = $this->input->post('name');
        }
         if ($this->input->post('place')) {
            $place = $this->input->post('place');
        }
        $vars['name'] = $name;
        $vars['place'] = $place;
        $this->load->view('add_sub_warehouse', $vars);
        }

	public function delete($code)
	{
	    $this->load->model('Warehouses_model');
	   // $this->load->model('Warehouses_product_model');
        $this->Warehouses_model->delete($code);
      //  $this->Warehouses_product_model->delete($code);
        $this->index();
	}
        public function delete_subalmacen($code)
	{
	    $this->load->model('Warehouses_model');
            $code = $this->input->post('code');
	   return  $this->Warehouses_model->delete_subalmacen($code);            
	}

	private function _validate_submit()
	{
	    $this->form_validation->set_rules('name', 'Nombre','trim|required');
	    $this->form_validation->set_rules('text', 'Dirección','trim|required');
	    return $this->form_validation->run();
	}


}